#define _CRT_SECURE_NO_WARNINGS 1
#pragma once
#include<iostream>
//#include <utility>
#include<assert.h>
template<class T>
class Vector
{
public:
	typedef T* iterator;
	typedef const T* const_iterator;
public:
	Vector() :start_(nullptr), finish_(nullptr), endofstorage_(nullptr)
	{
	}
	Vector(size_t n, const T& value = T())
		: start_(nullptr)
		, finish_(nullptr)
		, endofstorage_(nullptr)
	{
		reserve(n);
		while (n--)
		{
			push_back(value);
		}
	}
	Vector(int n, const T& value = T())
		: start_(new T[n])
		, finish_(start_ + n)
		, endofstorage_(finish_)
	{
		for (int i = 0; i < n; ++i)
		{
			start_[i] = value;
		}
	}
	//迭代器初始化构造函数
	template<class InputIterator>
	Vector(InputIterator first, InputIterator second)
	{
		size_t size = second - first;
		for (int i = 0; i < size; i++)
		{
			push_back(*first);
			first++;
		}
	}
	//拷贝 深拷贝
	Vector(const Vector& cp) :start_(nullptr), finish_(nullptr), endofstorage_(nullptr)
	{
		reserve(cp.capacity());
		const_iterator cpit = cp.const_begin();//这里要用常指针 因为传入的就是常对象 并且调用的是cp的常begin
		iterator it = begin();
		while (cpit != cp.const_end())
		{
			*it = *cpit;
			it++;
			cpit++;
		}
		finish_ = it;
	}
	Vector<T>& operator=(Vector<T>& cpv)//重载赋值 浅拷贝
	{
		swap(cpv);
		return *this;
	}
	~Vector()
	{
		if (start_)
		{
			delete[] start_;
			start_ = finish_ = endofstorage_ = nullptr;
		}
	}
	///////////////插入删除相关
	iterator insert(iterator pos, const T& value)//中间插入效率 低下
	{
		assert(pos <= finish_);//堆区自下往上生长！！！！！！！！！！！！！！！！！！！！
		if (finish_ == endofstorage_)
		{
			//先计算偏移量
			size_t offset = pos - start_;
			if (capacity() == 0)
			{
				reserve(1);
			}
			else
			{
				reserve(capacity() * 2);
			}
			//如果发生增容，需要重置pos
			pos = start_ + offset;
		}
		iterator end = finish_ - 1;
		while (end >= pos)//往后挪
		{
			*(end + 1) = *end;
			end--;
		}
		*pos = value;
		finish_++;
		return pos;
	}
	iterator erase(iterator pos)
	{
		assert(pos >= start_);
		//计算偏移量
		size_t offset = pos - start_;
		iterator end = pos + 1;
		while (end != finish_)
		{
			*(end - 1) = *end;
			end++;
		}
		finish_--;
		return pos;
	}
	void push_back(const T& value)
	{
		insert(end(), value);
	}
	void pop_back()
	{
		assert(finish_ > start_);
		erase(end()-1);
	}
	void swap(Vector<T>& v)
	{
		std::swap(start_, v.begin());
		std::swap(finish_, v.end());
		std::swap(endofstorage_, v.begin() + v.capacity());
	}
	///////////////空间开辟相关
	void reserve(size_t n)
	{
		if (n > capacity())
		{
			size_t oldsize = size();
			T* tmp = new T[n];
			if (start_)
			{
				for (size_t i = 0; i < oldsize; i++)
				{
					tmp[i] = start_[i];
				}
				delete[] start_;
				start_ = nullptr;
			}
			start_ = tmp;
			finish_ = start_ + oldsize;
			endofstorage_ = start_ + n;
		}
	}
	void resize(size_t n, const T& value = T())
	{
		if (n < size())
		{
			finish_ = start_ + n;
			return;
		}
		int count = n - size();
		while (count)
		{
			count--;
			push_back(value);
		}
	}
	size_t capacity()const
	{
		return endofstorage_ - start_;
	}
	const_iterator const_end()const
	{
		return finish_;
	}
	iterator end()//
	{
		return finish_;
	}
	const_iterator const_begin()const
	{
		return start_;
	}
	iterator begin()
	{
		return start_;
	}
	size_t size()
	{
		return finish_ - start_;
	}
	bool empty()
	{
		if (finish_ == start_)
		{
			return true;
		}
		return false;
	}
	///////////////元素访问
	T& operator[](size_t pos)
	{
		assert(pos < size());
		return *(start_ + pos);
	}
	const T& operator[](size_t pos)const
	{
		assert(pos < size());
		return *(start_ + pos);
	}
	T& front()
	{
		return *start_;
	}
	const T& front()const
	{
		return *start_;
	}
	T& back()
	{
		return *(finish_ - 1);
	}
	const T& back()const
	{
		return *(finish_ - 1);
	}
private:
	iterator start_;//指向数据开始
	iterator finish_;//指向有效数据结束
	iterator endofstorage_;//指向开辟的空间尾
};
int main()
{
	//未实现
	///////Vector<int> v = {1,2,3,4};
	// 
	// /////////////////////////
	//Vector<int>v1;
	//v1.push_back(1);
	//v1.push_back(2);
	//int n = 1;
	//for (int i=0;i<v1.size();i++)
	//{
	//	std::cout << v1[i] << std::endl;
	//}

	///////////////////
	//Vector<int>v2(10, 6);
	//for (int i=0;i<v2.size();i++)
	//{
	//	std::cout << v2[i] << std::endl;
	//}
	////////////////////
	Vector<int>v2(10, 6);
	Vector<int>v3(v2);
	//for (int i=0;i<v3.size();i++)
	//{
	//	std::cout << v3[i] << std::endl;
	//}
	//auto it = v3.begin();
	//while (it!=v3.end())
	//{
	//	std::cout << *it << " ";
	//	it++;
	//}
	//std::cout << std::endl;
	for (auto it:v3)
	{
		std::cout << it << " ";
		it++;
	}
	std::cout << std::endl;
	v3.pop_back();
	v3.push_back(9999);
	/////push 和 pop 基本都没有问题 那其他函数 都基本都可以
	for (auto it : v3)
	{
		std::cout << it << " ";
		it++;
	}
	std::cout << std::endl;
	return 0;
}